/**
 * 
 */
package com.kexinde.wjifen.action;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.kexinde.wjifen.dao.ActivityDao;
import com.kexinde.wjifen.dao.CommentDao;
import com.kexinde.wjifen.dao.ShopDao;
import com.kexinde.wjifen.model.Activity;
import com.kexinde.wjifen.model.Shop;
import com.kexinde.wjifen.util.OSCache;
import com.kexinde.wjifen.vo.IndexComment;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 已经废置
 * @author vincent
 * 
 */
@SuppressWarnings("serial")
public class IndexAction extends ActionSupport implements RequestAware,
		SessionAware, ApplicationAware {
	private final static Logger logger = Logger.getLogger(IndexAction.class);
	private Map<String, Object> request;
	private Map<String, Object> session;
	private Map<String, Object> application;

	/**
	 * 显示首页
	 */
	@SuppressWarnings("unchecked")
	public String execute() {
		try {
			List<Shop> hotShops = (List<Shop>) OSCache.getInstance().get("hotShops");
			List<Shop> newShops =(List<Shop>) OSCache.getInstance().get("newShops");
			List<Activity> activities = (List<Activity>) OSCache.getInstance().get("activities");
			List<IndexComment> comments = (List<IndexComment>) OSCache.getInstance().get("comments");
			
			if(hotShops==null){	//缓存过期，重新查询数据库，再写缓存
				hotShops = this.getHotShops();
				OSCache.getInstance().inputOSCache("hotShops", hotShops, 60);
			}
			if(newShops==null){	//同上
				newShops = this.getNewShops();
				OSCache.getInstance().inputOSCache("newShops", newShops, 60);
			}
			if(activities==null){	//同上
				activities = this.getActivities();
				OSCache.getInstance().inputOSCache("activities", activities, 60);
			}
			if(comments==null){	//同上
				comments = this.getComments();
				OSCache.getInstance().inputOSCache("comments", comments, 60);
			}
			
			List<Activity> bigActivities = activities.subList(0, 5);// 大图
			List<Activity> smallActivities = activities.subList(5, 11);// 小图
			application.put("hotShops", hotShops);
			application.put("newShops", newShops);
			application.put("bigActivities", bigActivities);
			application.put("smallActivities", smallActivities);
			application.put("comments", comments);
			return SUCCESS;

		} catch (Exception e) {
			logger.error(e);
		}
		return ERROR;
	}

	/**
	 * 获取16个热门商店
	 * 
	 * @return
	 */
	private List<Shop> getHotShops() {
		try {
			ShopDao shopDao = new ShopDao();
			return shopDao.queryHostShops();
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}

	/**
	 * 获取16个最新商店
	 * 
	 * @return
	 */
	private List<Shop> getNewShops() {
		try {
			ShopDao shopDao = new ShopDao();
			return shopDao.queryNewShops();
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}

	/**
	 * 获取首页11个商家活动
	 * 
	 * @return
	 */
	private List<Activity> getActivities() {
		try {
			ActivityDao activityDao = new ActivityDao();
			return activityDao.queryIndexActivities();
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}
	
	
	private List<IndexComment> getComments(){
		try {
			CommentDao commentDao = new CommentDao();
			return commentDao.queryIndexComments();
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}
	

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	@Override
	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}
}
